//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.HTMLEditorProvider;
using DotNetNuke.Services.Personalization;
namespace DotNetNuke.UI.UserControls
{
	[ValidationPropertyAttribute("Text")]
	public class TextEditor : System.Web.UI.UserControl
	{
		protected System.Web.UI.HtmlControls.HtmlTable tblTextEditor;
		protected System.Web.UI.HtmlControls.HtmlTableCell celTextEditor;
		protected System.Web.UI.WebControls.RadioButtonList optView;
		protected System.Web.UI.WebControls.TextBox txtDesktopHTML;
		protected System.Web.UI.WebControls.RadioButtonList optRender;
		protected System.Web.UI.WebControls.Panel pnlBasicTextBox;
		protected System.Web.UI.WebControls.Panel pnlBasicRender;
		protected System.Web.UI.WebControls.PlaceHolder plcEditor;
		protected System.Web.UI.WebControls.Panel pnlRichTextBox;
		protected System.Web.UI.HtmlControls.HtmlTableRow trView;
		protected DotNetNuke.UI.UserControls.LabelControl plView;
		private bool _ChooseMode = true;
		private bool _ChooseRender = true;
		private bool _HtmlEncode = true;
		private System.Web.UI.WebControls.Unit _Width;
		private System.Web.UI.WebControls.Unit _Height;
		private HtmlEditorProvider RichTextEditor;
		private string MyFileName = "TextEditor.ascx";
		public bool ChooseMode
		{
			get { return _ChooseMode; }
			set { _ChooseMode = value; }
		}
		public bool ChooseRender
		{
			get { return _ChooseRender; }
			set { _ChooseRender = value; }
		}
		public string DefaultMode
		{
			get
			{
				if (this.ViewState["DefaultMode"] == null
					|| String.IsNullOrEmpty(this.ViewState["DefaultMode"].ToString()))
				{
					return "RICH";
				}
				else
				{
					return this.ViewState["DefaultMode"].ToString();
				}
			}
			set
			{
				if (value.ToUpper() != "BASIC")
				{
					this.ViewState["DefaultMode"] = "RICH";
				}
				else
				{
					this.ViewState["DefaultMode"] = "BASIC";
				}
			}
		}
		public System.Web.UI.WebControls.Unit Height
		{
			get { return _Height; }
			set { _Height = value; }
		}
		public bool HtmlEncode
		{
			get { return _HtmlEncode; }
			set { _HtmlEncode = value; }
		}
		public string Mode
		{
			get
			{
				string strMode = "";
				UserInfo objUserInfo = UserController.GetCurrentUserInfo();
				if (objUserInfo.UserID >= 0)
				{
					if (Personalization.GetProfile("DotNetNuke.TextEditor", "PreferredTextEditor") != null)
					{
						strMode = Convert.ToString(Personalization.GetProfile("DotNetNuke.TextEditor", "PreferredTextEditor"));
					}
				}
				if (String.IsNullOrEmpty(strMode))
				{
					if (this.ViewState["DesktopMode"] != null
						&& !String.IsNullOrEmpty(this.ViewState["DesktopMode"].ToString()))
					{
						strMode = Convert.ToString(this.ViewState["DesktopMode"]);
					}
				}
				if (String.IsNullOrEmpty(strMode))
				{
					strMode = DefaultMode;
				}
				return strMode;
			}
			set
			{
				UserInfo objUserInfo = UserController.GetCurrentUserInfo();
				if (value.ToUpper() != "BASIC")
				{
					this.ViewState["DesktopMode"] = "RICH";
					if (objUserInfo.UserID >= 0)
					{
						Personalization.SetProfile("DotNetNuke.TextEditor", "PreferredTextEditor", "RICH");
					}
				}
				else
				{
					this.ViewState["DesktopMode"] = "BASIC";
					if (objUserInfo.UserID >= 0)
					{
						Personalization.SetProfile("DotNetNuke.TextEditor", "PreferredTextEditor", "BASIC");
					}
				}
			}
		}
		public string Text
		{
			get
			{
				if (optView.SelectedItem.Value == "BASIC")
				{
					switch (optRender.SelectedItem.Value)
					{
						case "T":
							return Encode(HtmlUtils.ConvertToHtml(txtDesktopHTML.Text));
						//break;
						case "R":
							return txtDesktopHTML.Text;
						//break;
						default:
							return Encode(txtDesktopHTML.Text);
						//break;
					}
				}
				else
				{
					return Encode(RichTextEditor.Text);
				}
			}
			set
			{
				if (!String.IsNullOrEmpty(value))
				{
					txtDesktopHTML.Text = Decode(HtmlUtils.ConvertToText(value));
					RichTextEditor.Text = Decode(value);
				}
			}
		}
		public string TextRenderMode
		{
			get { return Convert.ToString(this.ViewState["textrender"]); }
			set
			{
				string strMode;
				strMode = value.ToUpper().Substring(0, 1);
				if (strMode != "R" && strMode != "H" && strMode != "T")
				{
					strMode = "H";
				}
				this.ViewState["textrender"] = strMode;
			}
		}
		public System.Web.UI.WebControls.Unit Width
		{
			get { return _Width; }
			set { _Width = value; }
		}
		public HtmlEditorProvider RichText
		{
			get { return RichTextEditor; }
		}
		public string LocalResourceFile
		{
			get { return this.TemplateSourceDirectory + "/" + Services.Localization.Localization.LocalResourceDirectory + "/" + MyFileName; }
		}
		private string Decode(string strHtml)
		{
			if (this.HtmlEncode)
			{
				return Server.HtmlDecode(strHtml);
			}
			else
			{
				return strHtml;
			}
		}
		private string Encode(string strHtml)
		{
			if (this.HtmlEncode)
			{
				return Server.HtmlEncode(strHtml);
			}
			else
			{
				return strHtml;
			}
		}

		private void PopulateLists()
		{
			if (optRender.Items.Count == 0)
			{
				optRender.Items.Add(new ListItem(Services.Localization.Localization.GetString("Text", Services.Localization.Localization.GetResourceFile(this, MyFileName)), "T"));
				optRender.Items.Add(new ListItem(Services.Localization.Localization.GetString("Html", Services.Localization.Localization.GetResourceFile(this, MyFileName)), "H"));
				optRender.Items.Add(new ListItem(Services.Localization.Localization.GetString("Raw", Services.Localization.Localization.GetResourceFile(this, MyFileName)), "R"));
			}
			if (optView.Items.Count == 0)
			{
				optView.Items.Add(new ListItem(Services.Localization.Localization.GetString("BasicTextBox", Services.Localization.Localization.GetResourceFile(this, MyFileName)), "BASIC"));
				optView.Items.Add(new ListItem(Services.Localization.Localization.GetString("RichTextBox", Services.Localization.Localization.GetResourceFile(this, MyFileName)), "RICH"));
			}
		}
		private void SetPanels()
		{
			if (optView.SelectedIndex != -1)
			{
				Mode = optView.SelectedItem.Value;
			}
			if (!String.IsNullOrEmpty(Mode))
			{
				optView.Items.FindByValue(Mode).Selected = true;
			}
			else
			{
				optView.SelectedIndex = 0;
			}
			if (optRender.SelectedIndex != -1)
			{
				TextRenderMode = optRender.SelectedItem.Value;
			}
			if (!String.IsNullOrEmpty(TextRenderMode))
			{
				optRender.Items.FindByValue(TextRenderMode).Selected = true;
			}
			else
			{
				optRender.SelectedIndex = 0;
			}
			if (optView.SelectedItem.Value == "BASIC")
			{
				pnlBasicTextBox.Visible = true;
				pnlRichTextBox.Visible = false;
			}
			else
			{
				pnlBasicTextBox.Visible = false;
				pnlRichTextBox.Visible = true;
			}
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			RichTextEditor = HtmlEditorProvider.Instance();
			RichTextEditor.ControlID = this.ID;
			RichTextEditor.Initialize();
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			optRender.SelectedIndexChanged += new EventHandler(optRender_SelectedIndexChanged);
			optView.SelectedIndexChanged += new EventHandler(optView_SelectedIndexChanged);

			try
			{
				PopulateLists();
				UserInfo objUserInfo = UserController.GetCurrentUserInfo();
				RichTextEditor.Width = Width;
				RichTextEditor.Height = Height;
				txtDesktopHTML.Height = Height;
				txtDesktopHTML.Width = Width;
				tblTextEditor.Width = Width.ToString();
				celTextEditor.Width = Width.ToString();
				if (!ChooseMode)
				{
					trView.Visible = false;
				}
				if (!ChooseRender)
				{
					pnlBasicRender.Visible = false;
				}
				plcEditor.Controls.Add(RichTextEditor.HtmlEditorControl);
				SetPanels();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		protected void optRender_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			if (optRender.SelectedIndex != -1)
			{
				TextRenderMode = optRender.SelectedItem.Value;
			}
			if (Mode == "BASIC")
			{
				if (TextRenderMode == "H")
				{
					txtDesktopHTML.Text = HtmlUtils.ConvertToHtml(txtDesktopHTML.Text);
				}
				else
				{
					txtDesktopHTML.Text = HtmlUtils.ConvertToText(txtDesktopHTML.Text);
				}
			}
			SetPanels();
		}
		protected void optView_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			if (optView.SelectedIndex != -1)
			{
				Mode = optView.SelectedItem.Value;
			}
			if (Mode == "BASIC")
			{
				if (TextRenderMode == "T")
				{
					txtDesktopHTML.Text = HtmlUtils.ConvertToText(RichTextEditor.Text);
				}
				else
				{
					txtDesktopHTML.Text = RichTextEditor.Text;
				}
			}
			else
			{
				if (TextRenderMode == "T")
				{
					RichTextEditor.Text = HtmlUtils.ConvertToHtml(txtDesktopHTML.Text);
				}
				else
				{
					RichTextEditor.Text = txtDesktopHTML.Text;
				}
			}
			SetPanels();
		}
	}
}
